package com.yps.io.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * 阻塞式I/O创建的客户端
 * @author yps
 *
 */
public class Client {
	//默认端口和地址
	private static int DEFALUT_SERVER_PORT = 12344;
	private static String DEFALUT_SERVER_IP = "127.0.0.1";
	public static void send(String expression) throws IOException{
		send(DEFALUT_SERVER_PORT,expression);
	}
	private static void send(int port, String expression) throws IOException {
		System.out.println("算术表达式：" + expression);
		Socket socket = null;
		BufferedReader in = null;
		PrintWriter out = null;
		try {
			socket = new Socket(DEFALUT_SERVER_IP, port);
			out = new PrintWriter(socket.getOutputStream(),true);
			out.println(expression);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			socket.setSoTimeout(2000);
			System.out.println("__ 结果为：" + in.readLine());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//流使用要记得关闭
			if(in != null){
				in.close();
			}
			if(out != null){
				out.close();
			}
			if(socket != null){
				socket.close();
			}
		}
	}
	
}
